package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.ImagesMapper;
import com.bwie.mapper.IntroduceMapper;
import com.bwie.pojo.Ball;
import com.bwie.pojo.Images;
import com.bwie.pojo.Introduce;
import com.bwie.service.BallService;
import com.bwie.mapper.BallMapper;
import com.bwie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author lenovo
* @description 针对表【t_ball】的数据库操作Service实现
* @createDate 2024-09-04 15:03:34
*/
@Service
public class BallServiceImpl extends ServiceImpl<BallMapper, Ball>
    implements BallService{

    @Autowired
    private BallMapper ballmapper;

    @Autowired
    private IntroduceMapper introduceMapper;
    @Autowired
    private ImagesMapper imagesMapper;

    @Override
    public List<Ball> ballList() {
        return ballmapper.ballList();
    }

    @Transactional
    @Override
    public R addBall(Ball ball) {

        String ballNum ="MB"+ RandomUtil.randomNumbers(6);

        ball.setBallNum(ballNum);

        int i = baseMapper.insert(ball);

        Introduce introduce = new Introduce();
        introduce.setBallId(ball.getId());
        introduce.setIntroduceName(ball.getIntroduceName());

        introduceMapper.insert(introduce);

        for (Images images : ball.getImagesList()) {
            images.setBallId(ball.getId());

            imagesMapper.insert(images);
        }
        return R.OK("添加成功");
    }
}




